//=============================================================================
/**
 *
 *	@file		break_common.c
 *	@brief		汎用関数
 *	@author		hosaka genya
 *	@data		2008.11.13
 *
 */
//=============================================================================
#include "common.h"
#include "../pkthln_evt_info.naix"

#include "break_common.h"

#include "pkthev_break.naix"

//=============================================================================
/**
 *								定数定義
 */
//=============================================================================

//=============================================================================
/**
 *								構造体定義
 */
//=============================================================================

//=============================================================================
/**
 *							プロトタイプ宣言
 */
//=============================================================================

//=============================================================================
/**
 *								外部公開関数
 */
//=============================================================================

//-----------------------------------------------------------------------------
/**
 *	@brief	メイン用汎用CAP生成
 *
 *	@param	CATS_SYS_PTR csp
 *	@param	crp
 *	@param	px
 *	@param	py
 *	@param	anm
 *	@param	pri
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
CATS_ACT_PTR OBJ_AddCommon( CATS_SYS_PTR csp, CATS_RES_PTR crp, s16 px, s16 py, u16 anm, u16 pri )
{
	TCATS_OBJECT_ADD_PARAM_S prm = {
		0, 0, 0,
		0, 0, TCATS_ADD_S_PAL_AUTO, NNS_G2D_VRAM_TYPE_2DMAIN,
		{ CHR_ID_MAIN, PAL_ID_MAIN, CEL_ID_MAIN, ANM_ID_MAIN },
		0, 0,
	};

	CATS_ACT_PTR cap;

	GF_ASSERT(csp);
	GF_ASSERT(crp);

	prm.x = px;
	prm.y = py;
	prm.anm = anm;
	prm.pri = pri;
	prm.bg_pri = 1;

	cap = CATS_ObjectAdd_S_SubDistance( csp, crp, &prm, OBJ_DISTANCE );
	CATS_ObjectAutoAnimeSetCap( cap, TRUE );

	return cap;
}

//-----------------------------------------------------------------------------
/**
 *	@brief	LCD SUB用汎用CAP生成
 *
 *	@param	CATS_SYS_PTR csp
 *	@param	crp
 *	@param	px
 *	@param	py
 *	@param	anm
 *	@param	pri
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
CATS_ACT_PTR OBJ_AddCommonSub( CATS_SYS_PTR csp, CATS_RES_PTR crp, s16 px, s16 py, u16 anm, u16 pri )
{
	TCATS_OBJECT_ADD_PARAM_S prm = {
		0, 0, 0,
		0, 0, TCATS_ADD_S_PAL_AUTO, NNS_G2D_VRAM_TYPE_2DSUB,
		{ CHR_ID_SUB, PAL_ID_SUB, CEL_ID_SUB, ANM_ID_SUB },
		2, 0,
	};

	CATS_ACT_PTR cap;

	GF_ASSERT(csp);
	GF_ASSERT(crp);

	prm.x = px;
	prm.y = py;
	prm.anm = anm;
	prm.pri = pri;

	cap = CATS_ObjectAdd_S_SubDistance( csp, crp, &prm, OBJ_DISTANCE );
	CATS_ObjectAutoAnimeSetCap( cap, TRUE );

	return cap;
}

//-----------------------------------------------------------------------------
/**
 *	@brief ポケモンパラメータを取得＆換算
 *
 *	@param
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void SetBreakPokeParam( BREAK_POKE_PARAM* outParam, const u8 NetID, PKTHLN_PTR work )
{
	int i;
	BREAK_POKE_CONV_DATA conv_data;

	ArchiveDataLoad( &conv_data, ARC_PKTHEVT_PARAM, NARC_pkthln_evt_info_break_bin );

	for( i=0; i<ENTRY_MAX; i++ )
	{
		const POKE_SPEC* poke_spec;
		poke_spec = PTFrame_GetPokeSpec( work, NetID, i );

		GF_ASSERT( poke_spec->Power < CONV_PARAM_MAX );
		GF_ASSERT( poke_spec->Stamina < CONV_PARAM_MAX );
		GF_ASSERT( poke_spec->Technique < CONV_PARAM_MAX );

		outParam[i].Pow = conv_data.pow[ poke_spec->Power ];
		outParam[i].StaminaMax = conv_data.stamina[ poke_spec->Stamina ];
		outParam[i].Tec = conv_data.tec[ poke_spec->Technique ];
		outParam[i].Stamina = outParam[i].StaminaMax;

		HOSAKA_PRINT( "[%d]outParam[%d] pow=%d sta=%d tec=%d \n", NetID, i,
				outParam[i].Pow, outParam[i].Stamina, outParam[i].Tec );
	}
}

//-----------------------------------------------------------------------------
/**
 *	@brief	スタミナ 主処理
 *
 *	@param	BREAK_POKE_PARAM* Param
 *	@param	HealTimer
 *	@param	u8 pokeIdx
 *
 *	@retval
 */
//-----------------------------------------------------------------------------
void StaminaProc( BREAK_POKE_PARAM* bpp, const u32 HealTimer, const u8 PokeIdx )
{
	int i;

	// 回復
	for( i=0; i<ENTRY_MAX; i++ )
	{
		u16 now;
		u16 max;

		now = bpp[i].Stamina;
		max = bpp[i].StaminaMax;

		if( (HealTimer) != 0 && now < max )
		{
			if( i == PokeIdx )
			{
				// 操作ポケ
				if( (HealTimer) % STAMINA_HEAL_SYNC_NORMAL == 0 )
				{
					bpp[i].Stamina += STAMINA_HEAL_POINT_NORMAL;
				}
			}
			else
			{
				// 控えポケ
				if( (HealTimer) % STAMINA_HEAL_SYNC_BENCH == 0 )
				{
					bpp[i].Stamina += STAMINA_HEAL_POINT_BENCH;
				}
			}

			// 限界突破監視
			if( bpp[i].Stamina > max )
			{
				bpp[i].Stamina = max;
			}
		}
	}
}

//-----------------------------------------------------------------------------
/**
 *	@brief	スタミナ減算、ピヨリ判定
 *
 *	@param	const u32 Stamina
 *	@param	u16 AttackTimer
 *
 *	@retval	計算後の値
 */
//-----------------------------------------------------------------------------
u32 StaminaCalc( const u32 Stamina, const u16 AttackTimer )
{
	int val;

	GF_ASSERT( AttackTimer < ATTACK_SYNC );

	// マイナス値対策のため一旦代入
	val = Stamina - c_Attack_StaminaDecTbl[ AttackTimer ];

	// マイナス補正
	if( val <= 0 ){ val = 0; }

	return val;
}

//=============================================================================
/**
 *								static関数
 */
//=============================================================================

